import axios, { AxiosRequestConfig } from 'axios'

// Extend AxiosRequestConfig to include metadata
interface RequestConfigWithMetadata extends AxiosRequestConfig {
  metadata?: {
    startTime: Date;
  };
}

const api = axios.create({
  baseURL: '/api',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// API response time monitoring
api.interceptors.request.use((config: RequestConfigWithMetadata) => {
  config.metadata = { startTime: new Date() }
  return config
})

api.interceptors.response.use(response => {
  const config = response.config as RequestConfigWithMetadata
  const requestTime = new Date().getTime() - (config.metadata?.startTime.getTime() || 0)
  console.log(`Request time: ${requestTime}ms for ${response.config.url}`)
  return response
}, error => {
  return Promise.reject(error)
})

export default {
  // 药品管理
  getMedicines() {
    return api.get('/medicines/')
  },
  getMedicineByRfid(rfid: string) {
    return api.get(`/medicines/${rfid}`)
  },
  addMedicine(medicine: any) {
    return api.post('/medicines/', medicine)
  },
  queryMedicine(rfid: string) {
    return api.post('/medicines/query/', { rfid_id: rfid })
  },

  // 药品追溯
  getTraceHistory(rfid: string) {
    return api.get(`/trace/${rfid}`)
  },
  addTraceEvent(event: any) {
    return api.post('/trace/', event)
  },

  // 药品验证
  verifyMedicine(verification: any) {
    return api.post('/verify/', verification)
  },

  // 系统统计
  getSystemStats() {
    return api.get('/stats')
  }
} 